﻿/*
 *	Copyright (c) 2009 Queensland University of Technology. All rights reserved.
 *	The QUT Bioinformatics Collection is open source software released under the 
 *	Microsoft Public License (Ms-PL): http://www.microsoft.com/opensource/licenses.mspx.
 */
/*
 Scavenged on 15 Feb 2009, from http://silverlight.net/forums/p/13024/88467.aspx, in a post due to Kunal Chowdhury.
 */
using System;
using System.Windows.Browser;

namespace ClipboardDemo
{
    /// <summary>
    ///     This class is used to access the user's clipboard
    /// </summary>
    public static class Clipboard
    {
        /// <summary>
        ///     Set the text to the user's clipboard
        /// </summary>
        /// <param name="text">Text to set in the clipboard</param>
        public static void SetClipboardText(string text)
        {
            // get the user's clipboard
            var clipboard = (ScriptObject)HtmlPage.Window.GetProperty("clipboardData");
            
            if (clipboard != null)
            {
                // if clipboard is available, try to set the text to the clipboard
                bool success = (bool)clipboard.Invoke("setData", "text", text);
                if (!success)
                {
                    HtmlPage.Window.Alert("Unable to copy the text");
                }
            }
            else
            {
                HtmlPage.Window.Alert("Clipboard is unavailable");
            }
        }

        /// <summary>
        ///     Get the text from the user's clipboard
        /// </summary>
        /// <returns>Text present in user's clipboard</returns>
        public static string GetClipboardText()
        {
            string text = string.Empty;
            // get the user's clipboard
            var clipboard = (ScriptObject)HtmlPage.Window.GetProperty("clipboardData");

            if (clipboard != null)
            {
                // if clipboard is available, try to get the text from the clipboard
                text = (string)clipboard.Invoke("getData", "text");
            }
            else
            {
                HtmlPage.Window.Alert("Clipboard is unavailable");
            }
            return text;
        }
    }
}
